package com.game.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.roles.CardTalentConfig;
import com.coment.game.fight.Attribute;
import com.coment.game.fight.Attrs;
import com.coment.po.Game;
import com.coment.po.roles.Roles;
import com.game.dao.core.GameDao;
import com.game.dao.user.UserDao;
import com.game.dao.user.UserPlayInfoDao;
import com.game.dao.user.roles.RolesDao;
import com.game.logic.items.ItemsService;
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RequestMapping("/gm")
@RestController
@AllArgsConstructor
public class GMController {
    private static final String GM_NAME = "admin";
    private static final String GM_PASSWORD = "123456";
    private static String token;
    private final UserDao userDao;
    private final UserPlayInfoDao userPlayInfoDao;
    private final GameDao gameDao;
    private final ItemsService itemsService;
    private final RolesDao rolesDao;

    @Data
    @Getter
    public static class R<T> {
        private Integer code;
        private String message;
        private T data;

        public static <T> R<T> success(T data) {
            R<T> r = new R<>();
            r.setCode(200);
            r.setData(data);
            return r;
        }

        public static <T> R<T> error(String message) {
            R<T> r = new R<>();
            r.setCode(500);
            r.setMessage(message);
            return r;
        }
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @FieldDefaults(level = AccessLevel.PUBLIC)
    public static class Request {
        String name;
        String password;
    }

    @PostMapping("/login")
    public R<String> login(
            @RequestBody Request request
    ) {
        if (GM_NAME.equals(request.name) && GM_PASSWORD.equals(request.password)) {
            token = UUID.randomUUID().toString();
        } else {
            return R.error("用户名或密码错误");
        }
        return R.success(token);
    }


    /**
     * 用户信息
     *
     * @param id
     * @param name
     * @param playingRoomName
     */
    public record UserInfo(
            Integer id,
            String name,
            Integer playingRoomName,
            long recentPlaytime
    ) {
    }


    ///  查看活跃的直播间 信息
    @GetMapping("/activeRoom")
    public R<List<Game>> activeRoom() {
        List<Game> allByStartTimeNotNull = gameDao.findAllByStartTimeNotNull(0L);
        log.info("activeRoom: {}", allByStartTimeNotNull);
        return R.success(allByStartTimeNotNull);
    }


    /// 查找用户
    @GetMapping("/findUser")
    public R<List<UserInfo>> findUser(@RequestParam String name) {
        List<com.coment.po.UserInfo> all = userDao.findAllByNameLike(name);
        log.info("findUser: {}", all);

        return R.success(all.stream().map(userInfo -> new UserInfo(
                userInfo.getId(),
                userInfo.getName(),
                200000,
                20000000
        )).toList());
    }

    /// 查看用户信息
    @GetMapping("/userInfo")
    public R<List<UserInfo>> userInfo() {
        List<com.coment.po.UserInfo> all = userDao.findAll();
        return R.success(all.stream().map(userInfo -> new UserInfo(
                userInfo.getId(),
                userInfo.getName(),
                200000,
                20000000
        )).toList());
    }

    /// 查看物品分类
    @GetMapping("/itemsCategory")
    public R<Map<String, Integer>> itemsCategory() {

        Map<String, Integer> data = Map.of(
                "礼包", 0,
                "货币", 1,
                "装备", 2,
                "宠物", 3,
                "洞府", 4,
                "道具", 4,
                "称号", 5,
                "角色", 6,
                "经验",7
        );
        return R.success(data);
    }


    public record SendItem(
            Integer[] userIds,
            List<ItemsService.ObtainItem> obtainItemList
    ) {
    }

    /// 获取道具
    @PostMapping("/sendItem")
    public R<Void> sendItem(
            @RequestBody SendItem sendItem
    ) {
        log.info("sendItem: {}", sendItem);
        itemsService.doObtainItem(sendItem.userIds, sendItem.obtainItemList);
        return R.success(null);
    }

    /// 查看角色列表
    @GetMapping("/rolesList")
    public R<List<CardTalentConfig>> rolesList() {
        return R.success(Tables.cfg.get_tbcardtalentconfig().getDataList());
    }



    /// 查看玩家当前的角色面板
    @GetMapping("/roles")
    public R<?> roles(Integer userId){
        Roles roles = rolesDao.findByIdMust(userPlayInfoDao.findByUserInfoIdOrCreate(userId).recentlyPlayedRolesId);
        Attrs value = roles.rolePanel.cultivatePanel.getValue();
        StringBuilder logInfo = Attribute.getLogInfo(value);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(roles);
        stringObjectMap.put("属性详情", logInfo);
        String string = JSONObject.toJSONString(stringObjectMap);
        return R.success(string);
    }
}
